package com.wzdigit.wms.wms.service.board;

import com.wzdigit.wms.basic.enums.GdnDtlStatusEnum;
import com.wzdigit.wms.basic.enums.GdnDtlUdf1Enum;
import com.wzdigit.wms.basic.enums.GdnHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.GdnTypeEnum;
import com.wzdigit.wms.wms.domain.borad.GdnFinishInfoDto;
import com.wzdigit.wms.wms.mapper.board.GdnBoardMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 出库看板接口
 * Created by zyf on 2021/9/22 16:21
 */
@Service
@Slf4j
public class GdnBoraderService {
    private static final List<Integer> headerStatus= Arrays.asList(GdnHeaderStatusEnum.FINISH.code,
            GdnHeaderStatusEnum.CLOSE.code);
    private static final String UDF_YES=GdnDtlUdf1Enum.YES.code;
    private static final String GDN_TYPE= GdnTypeEnum.D01.code;
    private static final Integer DTL_PICK_STATUS=GdnDtlStatusEnum.PICKING.code;
    private static final Integer WAIT_PICK_STATUS=GdnDtlStatusEnum.WAITPICK.code;

    @Autowired
    private GdnBoardMapper gdnBoardMapper;

    //查询发货完成情况
    //(左)中的数据差异数在装柜中的数据  TODO:待讨论解决方案
    public GdnFinishInfoDto searchFinishInfo(Date beginSplitTime,Date endSplitTime){
        GdnFinishInfoDto dto=new GdnFinishInfoDto();
        //装柜数(左):已分库(UDF1=YES)的GDN_HEADER条数
        Integer allCtnrCount = gdnBoardMapper.selectCtnrCount(null, UDF_YES, GDN_TYPE, beginSplitTime, endSplitTime);
        dto.setAllCtnrCount(allCtnrCount);
        //已装柜(左):已分库(UDF1=YES)且STATUS=4/5的GDN_HEADER条数
        Integer ctnrCount = gdnBoardMapper.selectCtnrCount(headerStatus, UDF_YES, GDN_TYPE, beginSplitTime, endSplitTime);
        dto.setCtnrCount(ctnrCount);
        //已拣货(左):已经所有明细为拣货中且主表STATUS!=4/5的条数
        //换算：allCtnrCount-(存在明细不是非拣货中或者STATUS=4/5)
        Integer pickCount=allCtnrCount-gdnBoardMapper.selectNotPickCount(null,DTL_PICK_STATUS, UDF_YES, GDN_TYPE, beginSplitTime, endSplitTime);
        dto.setPickCount(pickCount);
        //出库单下存在待拣货的明细
        Integer waitPickCount = gdnBoardMapper.selectWaitPickCount(WAIT_PICK_STATUS, UDF_YES, GDN_TYPE, beginSplitTime, endSplitTime);
        dto.setWaitPickCount(waitPickCount);
        //任务完成率
        String ctnrTcr=ctnrCount/allCtnrCount*100+"%";
        dto.setCtnrTcr(ctnrTcr);

        return dto;
    }


}
